import App from './App.vue'
import pinia from '@/framework/store'
import router, {enterDynamicRoute} from '@/framework/router'
import {createApp} from 'vue'
import STable from '@surely-vue/table'
import '@/framework/assets/css/base.css'
import VueLazyload from 'vue-lazyload'
import '@surely-vue/table/dist/index.less'
import pluginLoader from '@/framework/plugin'
import {download, upload} from '@/framework/network/request'
//自己封装的，用于显示antd Menu的图标组件
import {setupAntdIcon} from '@/framework/components/common/antdIcons/AntdIcons'
import defaultAvatar from '@/framework/assets/imgs/defaultAvatar.png'
import {useCommonStore} from '@/framework/store/common'
import {checkLoginState} from '@/framework/network/login'

const app = createApp(App)
pluginLoader(app)
// 因为在使用routeStore之前，需要初始化pinia，所以需要把这句话单独拿出来
app.use(pinia)
app.use(router)
app.use(STable)
app.use(VueLazyload, {loading: defaultAvatar})
app.provide('upload', upload)
app.provide('download', download)
setupAntdIcon(app)
app.mount('#app')

router.beforeEach(async (to, from, next) => {
    const commonStore = useCommonStore(pinia)
    console.debug('Router Guard', to, from, next)
    if (commonStore.hasLogin || to.path === '/login') {
        enterDynamicRoute(to, from, next)
    } else {
        await checkLoginState()
        delete to.query.id_token
        next({path: to.path, query: to.query})
    }
})

export default app
